<?php
//include('../lib/log4php/Logger.php');
//Logger::configure('../log/logconfig.xml');

include 'DatabaseDefine.php';
class UserLoginModel {
    
    private $log;
    public function __construct(){
       $this->log = Logger::getLogger(__CLASS__); 
    }
    
    private $userId;
    private $password;
    
    public function getUserId(){
        return $this->userId;
    }
    
    public function setUserId($userId){
        $this->userId = $userId;
    }
    
    public function getPassword(){
        return $this->password;
    }
    
    public function setPassword($password){
        $this->password = $password;
    }
    
    public function toString(){
        return "UserLoginModel To String[userId=".$this->userId.", password=".$this->password."]";
    }
    
    // Begin Model Opration
    public function userLogin(){
        try{
            // Start Transaction
            
            // Begin Open Connection
            include 'DatabaseOpenConnection.php';
            // End Open Connection
            // Begin Query1
            $sql1 = "SELECT userId FROM ".TB_USERLOGIN." WHERE userId ='".$this->userId."' AND password = '".$this->password."' ";
            $this->log->debug($sql1);
            $result1 = mysql_query($sql1);
            
            if (!$result1) {
                $errmsg = "Invalid query . ". mysql_error();
                $this->log->error($errmsg);
                throw new Exception($errmsg);
            }
            // End Query1
            // Begin Num Row
            $num_rows = mysql_num_rows($result1);
            // End Num Row
            // Begin Clode Connection
            include 'DatabaseCloseConnection.php';
            // End Close Connection
            // End Transaction
            
            if($num_rows <= 0){
                $dateTime = date("Y-m-d h:i:s A");
                $refId = rand(10,10000000);
                $response = array();
                $response['loginResponseHeader'] = array( 'refId' => $refId, 'dateTime' => $dateTime,'responseCode'=>'1212','responseDesc'=>'Data Not Found');
                $response['loginResponseDetail'] = array( 'firstname' => '', 'lastname' => '','rolelist'=>'');
                return $response;
            }
            

            $dateTime = date("Y-m-d h:i:s A");
            $refId = rand(10,10000000);
            $response = array();
            $response['loginResponseHeader'] = array( 'refId' => $refId, 'dateTime' => $dateTime,'responseCode'=>'0','responseDesc'=>'success');
            $response['loginResponseDetail'] = array( 'firstname' => '', 'lastname' => '','rolelist'=>'');
            
            return $response;
        }catch(Exception $e){
            // Begin Clode Connection
            include 'DatabaseCloseConnection.php';
            // End Close Connection
            throw $e;
        }
    }
    
    // End Model Operation
}

/*
$login = new UserLoginModel();
$login->setUserId("aot.maha@gmail.com");
$login->setPassword("password");
echo $login->toString()."<br />";
print_r($login->userLogin());
*/
?>
